﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Globalization;
using System.Windows.Controls;
using ScrumTable.Common;

#endregion

namespace ScrumTable.UI.View.Rule
{

    /// <summary>
    /// Rule for checking if  value can be converted in a given type
    /// </summary>
    public class ChangeConvertTypeRule : ValidationRule
    {
        /// <summary>
        /// Checks if the new value can be converted in this type
        /// </summary>
        public Type Type { get; set; }

        /// <summary>
        /// Validate is called when Data binding is updating
        /// </summary> 
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            try
            {
                value.ChangeTypeWithNullableString(Type);
            }
            catch (Exception e)
            {
                return new ValidationResult(false, e.Message);
            }

            return new ValidationResult(true, null);

        }
    }
}
